package com.wenzheng.quartz.config;

import com.wenzheng.quartz.service.*;
import org.springframework.beans.factory.annotation.*;
import org.springframework.context.*;
import org.springframework.context.event.*;
import org.springframework.stereotype.*;

import java.util.concurrent.*;

/**
 * Spring容器启动监听器
 * @author
 * @version 1.0
 * @blog
 */
@Component
public class StartupListener implements ApplicationListener<ContextRefreshedEvent> {
	/**
	 * 这个标识主要用来解决 onApplicationEvent方法被多次调用的问题
	 */
	private static boolean isRun = false;
	@Autowired
	private TaskItemService service;
	ExecutorService cachedThreadPool = Executors.newCachedThreadPool();

	/**
	 * Spring容器启动后，会执行该方法
	 */
	@Override
	public void onApplicationEvent(ContextRefreshedEvent evt) {
		if (!isRun) {
			try {
				isRun = true;
				//  开启线程去初始化task,将数据库中的任务添加到quartz中
				cachedThreadPool.execute(() -> service.initTask());
			}catch (Exception e) {
			}
		}
	}
}
